package eclipse.lib.scripting;
/**
 * The ByteArray class exists as an array of bytes with extended modification values.
 */
public class ByteArray extends ByteInterpreter
{
	private byte[] byteArray;
	/** Returns the size of the byte array */
	public int length()
	{
		return byteArray.length;
	}
	/** Creates a new byte array */
	public ByteArray()
	{
		byteArray = new byte[0];
	}
	/**
	 * Creates a new byte array from an array of bytes.
	 * @param newArray Array of bytes to make the array from.
	 */
	public ByteArray(byte[] newArray)
	{
		byteArray = newArray;
	}
	/**
	 * Adds a byte to the end of the byte array
	 * @param newByte Byte to add
	 */
	public void appendTo(byte newByte)
	{
		byte[] newArray = new byte[1];
		newArray[0] = newByte;
		byteArray	= appendToByteArray(byteArray, newArray);
		
	}
	/**
	 * Adds an integer to the end of the byte array
	 * @param newInt Integer to add
	 */
	public void appendTo(int newInt)
	{
		byte[] newArray;
		newArray 	= ByteInterpreter.intToByteArray(newInt);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a long to the end of a byte array
	 * @param newLong Long to add
	 */
	public void appendTo(long newLong)
	{
		byte[] newArray;
		newArray	= ByteInterpreter.longToByteArray(newLong);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a float to the end of a byte array
	 * @param newFloat Float to add
	 */
	public void appendTo(float newFloat)
	{
		byte[] newArray;
		newArray	= ByteInterpreter.floatToByteArray(newFloat);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a double to the end of a byte array
	 * @param newDouble Double to add
	 */
	public void appendTo(double newDouble)
	{
		byte[] newArray;
		newArray	= ByteInterpreter.doubleToByteArray(newDouble);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a String to the end of a byte array
	 * @param newString String to add
	 */
	public void appendTo(String newString)
	{
		byte[] newArray;
		newArray	= ByteInterpreter.stringToByteArray(newString);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a character to the end of a byte array
	 * @param newChar Char to add
	 */
	public void appendTo(char newChar)
	{
		byte[] newArray;
		newArray	= ByteInterpreter.charToByteArray(newChar);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds an array of bytes to the end of a byte array
	 * @param newArray Bytes to add
	 */
	public void appendTo(byte[] newArray)
	{
		byteArray = appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds an array of bytes to a specified location of the byte array
	 * @param newArray Bytes to add
	 * @param offset Location to add to
	 */
	public void appendTo(byte[] newArray, int offset)
	{
		byteArray = appendToByteArray(byteArray, newArray, offset);
	}	
	/**
	 * Returns a new ByteArray from a section of the ByteArray
	 * @param start Start location
	 * @param end End location
	 */
	public ByteArray subArray(int start, int end)
	{
		return new ByteArray(ByteInterpreter.getSubByteArray(byteArray, start, end));
	}
	/**
	 * Returns an array of bytes representing the byte array
	 * @return
	 */
	public byte[] toArray()
	{
		return byteArray;
	}
	/**
	 * Overwrites the byte array with an array of bytes
	 * @param newArray Array of bytes to overwrite the byte array
	 */
	public void setArray(byte[] newArray)
	{
		byteArray = newArray;
	}	
}
